from qiniu import Auth, QiniuMacAuth
import base64
import requests


def urlsafe_base64_encode(data):
    return base64.urlsafe_b64encode(data.encode()).decode()


# 初始化认证实例
access_key = 'fc8Xlj-PmGhXa2VKzDbIHFRpJtRH9u40oqqs5oxN'
secret_key = 'sieJ6Dr8VSyE64T6qI2EzVMvnO-Os6ioxkSAgbX4'

auth = QiniuMacAuth(access_key, secret_key)

# 定义 bucket 和 key
bucket = "juruo"
key = "your_key"

# 创建 EncodedEntryURI
entry_uri = f"{bucket}:{key}"
encoded_entry_uri = urlsafe_base64_encode(entry_uri)

# 定义 URL 和 Host
url = f"http://rsf.qiniuapi.com/list?bucket={bucket}"
headers = {"Host": "rsf.qiniuapi.com"}

# 生成管理凭证
authorization_token = auth.token_of_request(
    method="GET",
    host=headers['Host'],
    url=url,
    qheaders=auth.qiniu_headers(headers))

headers['Authorization'] = f'Qiniu {authorization_token}'

# 发送 GET 请求
response = requests.get(url, headers=headers)

# 输出响应
print(response.status_code)
print(response.json())
